<template>
  <div class="search-result-container">
    <div class="search-header">
      <h2>搜索结果：<span class="keyword">{{ keyword }}</span></h2>
    </div>

    <!-- 没有结果 -->
    <div v-if="results.length === 0" class="no-results">
      <p>没有找到与“{{ keyword }}”相关的商品</p>
      <button @click="$router.push('/')">返回首页</button>
    </div>

    <!-- 有结果 -->
    <div v-else>
      <div class="goods-list">
        <div v-for="item in paginatedItems" :key="item.id" class="goods-card">
          <img :src="item.image" alt="商品图片" class="goods-image" />
          <div class="goods-info">
            <h3 class="goods-name">{{ item.name }}</h3>
            <p class="goods-price">￥{{ item.price }}</p>
            <RouterLink :to="`/product/${item.id}`" class="btn-detail">查看详情</RouterLink>
          </div>
        </div>
      </div>

      <!-- 分页组件 -->
      <div class="pagination-container">
        <el-pagination
            layout="prev, pager, next"
            :total="results.length"
            :page-size="itemsPerPage"
            @current-change="handlePageChange"
            background
            small
        />
      </div>
    </div>
  </div>
</template>


<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import { getShoeByName } from '@/apis/shoes'

const route = useRoute()
const keyword = ref(route.query.keyword || '')
const results = ref([])
const currentPage = ref(1)
const itemsPerPage = ref(12) // 每页显示 9 条

onMounted(async () => {
  if (keyword.value) {
    const res = await getShoeByName(keyword.value)
    if (res.code === 0) {
      results.value = res.data || []
    }
  }
})

// 计算当前页的商品
const paginatedItems = computed(() => {
  const start = (currentPage.value - 1) * itemsPerPage.value
  return results.value.slice(start, start + itemsPerPage.value)
})

// 切换页码
const handlePageChange = (page) => {
  currentPage.value = page
}
</script>


<style scoped lang="scss">
$primary-color: #2c3e50;
$accent-color: #d4a373;

.search-result-container {
  max-width: 1200px;
  margin: 60px auto;
  padding: 20px;
}

.search-header {
  text-align: left;
  margin-bottom: 30px;

  h2 {
    font-size: 24px;
    color: $primary-color;
  }

  .keyword {
    color: $accent-color;
    font-weight: bold;
  }
}

.no-results {
  text-align: center;
  padding: 60px 0;
  background: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

  p {
    font-size: 18px;
    color: #666;
    margin-bottom: 20px;
  }

  button {
    background-color: $accent-color;
    color: white;
    border: none;
    padding: 10px 20px;
    font-size: 16px;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.3s ease;

    &:hover {
      background-color: darken($accent-color, 10%);
    }
  }
}

.goods-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 20px;
}

.goods-card {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  padding: 15px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  }

  .goods-image {
    width: 100%;
    height: 180px;
    object-fit: cover;
    border-radius: 6px;
    margin-bottom: 12px;
    transition: transform 0.3s ease;

    &:hover {
      transform: scale(1.03);
    }
  }

  .goods-info {
    padding: 10px 0;

    .goods-name {
      font-size: 16px;
      color: $primary-color;
      margin-bottom: 8px;
    }

    .goods-price {
      color: #e60012;
      font-size: 18px;
      font-weight: bold;
      margin-bottom: 10px;
    }

    .btn-detail {
      display: inline-block;
      color: white;
      background: $accent-color;
      padding: 6px 12px;
      font-size: 14px;
      border-radius: 4px;
      text-decoration: none;
      transition: background 0.3s ease;

      &:hover {
        background: darken($accent-color, 10%);
      }
    }
  }
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
  margin-bottom: 60px;

  :deep(.el-pagination.is-background .el-pager li:not(.disabled).active) {
    background-color: $accent-color !important;
    color: white;
  }

  :deep(.el-pagination.is-background .el-pager li:hover) {
    color: $accent-color;
  }

  :deep(.el-pagination__jump) {
    margin-left: 10px;
  }
}
</style>

